<?
session_start();
include 'php/connect.php';
include 'php/functions.php';

$pId =  mysql_real_escape_string($_POST['parentId']);
$amount =  mysql_real_escape_string($_POST['amount']);



if (isset($_SESSION['userId'])){
	$canWrite = true;
}else{
	$canWrite = false;
}

if (isset($_POST['parentId']) && isset($_POST['amount'])){


	$abfrage = "INSERT INTO ffxivc_memory ( 
										parentId
										, userId
										, type
										, cdate ) 
										VALUES (
										\"".$pId."\" 
										,\"".$_SESSION['userId']."\" 
										,'MSG' 
										,\"".date('Y-m-d H:i:s')."\"
										)ON DUPLICATE KEY UPDATE cdate=\"".date('Y-m-d H:i:s')."\"
										";
	$res = mysql_query($abfrage);

	$output = "
				<script type='text/javascript'>
										
					$(document).ready(function(){
						$('.more-comment').click(function(e) {
							loadMessage(".$pId.",".($amount+5).");
						});
					});
				</script>
				<table id='message_convo' class='content-subtitle-table'>";
				
					
				$abfrage = "SELECT 
					msg.messageId
					,msg.fromId
					,msg.toId
					,msg.title
					,msg.message
					,fromUsers.userName as fromUser
					,fromUsers.avatar as fromAv
					,toUsers.userName as toUser
					,toUsers.avatar as toAv
					, (CASE WHEN msg.parentId=0 THEN msg.cdate ELSE msg.udate END) as writeDate
					FROM ((
					ffxivc_messages as msg 
					LEFT JOIN 
					ffxivc_users as fromUsers
					ON msg.fromId = fromUsers.userId) 
					LEFT JOIN
					ffxivc_users as toUsers
					ON msg.toId = toUsers.userId) 
					WHERE (msg.parentId = \"".$pId."\" OR msg.messageId = \"".$pId."\")
					ORDER BY (CASE WHEN msg.parentId=0 THEN msg.cdate ELSE msg.udate END) DESC
					LIMIT 0, ".($amount+1)."
					";	
				
				$res = mysql_query($abfrage);
				$num = mysql_num_rows($res);	
				$i = 1;
				
				//$temp_fromUser = mysql_result($res, 0, "fromUser"); 
				//$temp_toUser = mysql_result($res, 0, "toUser"); 
				
				

				
				while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
					if($canWrite && $i == 1){
						if($_SESSION['userId'] == $row['fromId']){
							$replyToUser = $row['toUser'];
						}else{
							$replyToUser = $row['fromUser'];
						}
						if($_SESSION['avatar'] != "" && $_SESSION['avatar'] != NULL){
							$MyAvatar = $_SESSION['avatar'];
						}else{
							$MyAvatar = "media/no-avatar.png";
						}
						$output .= "
						<tr class='spec-row tr-table border-bottom spec-detail-list-selected'>
							<td class='td-table ver-top cur-point' style='padding-right: 10px; padding-left: 10px;'>
								<a class='cur-point' name='no' href=\"javascript:getUser('".$_SESSION['userName']."');\"><div class='border-av'><img class='avatar-pic' src='".$MyAvatar."?".time()."'/></div></a>
							</td>
							<td id='spec-comment-write-container' class='td-table ver-top pad-t-12' style='padding-right: 22px; padding-left: 5px; padding-top: 15px;'>
								<textarea id='write-comment' type='text'>Reply to ".$replyToUser."...</textarea>
							</td>
						</tr>
						<script>
							var shiftPressed = false;
							$(window).keydown(function(evt) {
							  if (evt.which == 16) { // shift
								shiftPressed = true;
							  }
							}).keyup(function(evt) {
							  if (evt.which == 16) { // shift
								shiftPressed = false;
							  }
							});
							$('#write-comment').keypress(function(event) {
								if (event.which == 13 && !(shiftPressed)){
									sendReply('".$pId."', '".$replyToUser."', $(this).val())
									event.preventDefault();
								}
							});

							$('#write-comment').focus(function(){
								$(this).val('');
							});
						</script>";
					}
				
				
					if($row['fromAv'] != ""){
						$MyAvatar = "<div class='border-av'><img class='avatar-pic' src='".$row['fromAv']."?".time()."'/></div>";
					}else{
						$MyAvatar = "<div class='border-av'><img class='avatar-pic' src='media/no-avatar.png'/></div>";
					}
					$output .= "<tr id='tr_".$row['messageId']."' class='spec-row tr-table ";
					if($i != $amount && $i!=$num){
						$output .= "border-bottom";
					}
					$output .= "' name='".$row['messageId']."'>
						<td class='td-table ver-top cur-point' style='padding-left: 10px; padding-right: 10px;'>
							<a class='cur-point' name='no' href=\"javascript:getUser('".$row['fromUser']."');\">".$MyAvatar."</a>
						</td>
						<td class='td-table ver-top pad-t-12'>
							<div style='height:15px;'>
								<a class='cur-point fl-left' name='no' href=\"javascript:getUser('".$row['fromUser']."');\">".$row['fromUser']."</a>
							</div>
							<div class='spec-comment normal-text pad-5' style='overflow: hidden;white-space: pre-wrap;'>".$row['message']."</div>
							<div class='normal-text pad-5 font-10 fl-left'>
							<div class='op-4 fl-left' style='padding-bottom:5px; padding-right:5px;'>".$row['writeDate']."</div>
							";
								
					
					/** UNCOMMENT TO ALLOW DELETION **/
					/*if($_SESSION['userName'] == $row['userName'] && $row['type'] != "RATE"){
						$output .= 	"<div name='no' class='normal-text bold-text grey-text pad-lr-5 fl-left ' style='padding-top:2px;'>*</div>
								<a id='".$row['id']."' class='delete-comment fl-left pad-lr-5 font-10' style='cursor: pointer;' name='no' >Delete</a>";
					}*/
					
					$output .= "</div>
							</div>
						</td>
					</tr>";
					if($i == $amount){
						break;
					}
					$i++;
				}
				
				if($i<$num){
					// MORE COMMENTS TO SHOW
					$output .= "<tr>
						<td  colspan='2' style='text-align: center;'>
							<div class='more-comment center cur-point arrow-down' style='display:inline;'/>
						</td>
					</tr>";
					
				}

				mysql_free_result($res);
			
		$output .= "</table><div style='clear:both;'>";

	echo  $output;
}

?>